/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.tools.domain


import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.persistence.*
import me.zhengjie.common.utils.JacksonUtil

import org.hibernate.annotations.UpdateTimestamp

import java.sql.Timestamp

/**
 * 上传成功后，存储结果
 * @author Zheng Jie
 * @date 2018-12-31
 */
@Entity
@Table(name = "tool_qiniu_content")
data class QiniuContent (
    @Id
    @Column(name = "content_id")
    @Schema(description ="ID", hidden = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var  id: Long? = null,

    @Column(name = "name")
    @Schema(description ="文件名")
    var  key: String? = null,

    @Schema(description ="空间名")
    var  bucket: String? = null,

    @Schema(description ="大小")
    var  size: String? = null,

    @Schema(description ="文件地址")
    var  url: String? = null,

    @Schema(description ="文件类型")
    var  suffix: String? = null,

    @Schema(description ="空间类型：公开/私有")
    var  type: String = "公开",

    @UpdateTimestamp
    @Schema(description ="创建或更新时间")
    @Column(name = "update_time")
    var  updateTime: Timestamp? = null
){
    annotation class Update

    companion object {
        fun fromJson(json: String): QiniuContent? {
            val obj= JacksonUtil.parseObject(json, QiniuContent::class.java)!!
            return when(obj){
                is QiniuContent -> obj
                else -> null
            }
        }
    }
    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }


    fun copy(source: QiniuContent?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}